home *** CD-ROM | disk | FTP | other *** search
/ Giga Games 1 / Giga Games.iso / net / vir_real / multi102.taz / multi102 / multiverse-1.0 / extensions / ext_biwing.c next >
Encoding:
C/C++ Source or Header  |  1993-04-01  |  10.2 KB  |  316 lines

  1.  
  2. /**
  3.  
  4. #     #                                 #     #
  5. ##   ##  #    #  #        #####     #   #     #  ######  #####    ####   ######
  6. # # # #  #    #  #          #       #   #     #  #       #    #  #       #
  7. #  #  #  #    #  #          #       #   #     #  #####   #    #   ####   #####
  8. #     #  #    #  #          #       #    #   #   #       #####        #  #
  9. #     #  #    #  #          #       #     # #    #       #   #   #    #  #
  10. #     #   ####   ######     #       #      #     ######  #    #   ####   ######
  11.  
  12.  #####
  13. #     #   #   #   ####    #####  ######  #    #
  14. #          # #   #          #    #       ##  ##
  15.  #####      #     ####      #    #####   # ## #
  16.       #     #         #     #    #       #    #
  17. #     #     #    #    #     #    #       #    #
  18.  #####      #     ####      #    ######  #    #
  19.  
  20.     Copyright: Robert Grant, 1993
  21.  
  22. **/
  23.  
  24. #define _XOPEN_SOURCE
  25. #define _ALL_SOURCE
  26.  
  27. /* includes */
  28.  
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <X11/Xlib.h>
  32. #include <X11/keysym.h>
  33. #include <math.h>
  34. #include <stdlib.h>
  35.  
  36. #include <graphics.h>
  37. #include <messages.h>
  38. #include <eventHandler.h>
  39. #include <clientManager.h>
  40. #include <objectManager.h>
  41. #include <userInterface.h>
  42.  
  43. #include <ext_biwing.h>
  44.  
  45. /* Globals */
  46.  
  47. /* Macros */
  48.  
  49. int GetButtonPress(int mask, int *speed) {
  50.  
  51.     if (mask & Button2Mask)
  52.         return(TRUE);
  53.     if ((mask & Button1Mask) && (*speed < MAXSPEED))
  54.         *speed += 1;
  55.     if ((mask & Button3Mask) && (*speed > MINSPEED))
  56.         *speed -= 1;
  57.     return(FALSE);
  58.     }
  59.  
  60. void regenerateShield(int *shield) {
  61.     static int regen = 0;
  62.     if (*shield < 40)
  63.         regen++;
  64.     if (regen == 50 && *shield < 40) {
  65.         (*shield)++;
  66.         regen = 0;
  67.         }
  68.     }
  69.  
  70. void moveBiwing(object_t *theBiwing, biwingExt_t *biwingData,
  71.                                     move_t *event) {
  72.  
  73.     serverEventMsg_t ev;
  74.     changePart_t part;
  75.     changePanel_t panel;
  76.     moveEvent_t move;
  77.     int speed = biwingData->speed;
  78.     int shields = biwingData->shields;
  79.     double temp = biwingData->temp;
  80.     memset(&ev, 0, sizeof(serverEventMsg_t));
  81.     memset(&part, 0, sizeof(changePart_t));
  82.     theBiwing->coords.loc.x -= sin(RADIANS(theBiwing->coords.rot.y)) * speed/8.0;
  83.     theBiwing->coords.loc.y -= sin(RADIANS(theBiwing->coords.rot.x)) * speed/8.0;
  84.     theBiwing->coords.loc.z += cos(RADIANS(theBiwing->coords.rot.y)) * speed/8.0;
  85.  
  86.     regenerateShield(&biwingData->shields);
  87.     biwingData->temp += ((biwingData->speed - 30) / 10.0);
  88.     if (biwingData->temp < 0) biwingData->temp = 0;
  89.     if (biwingData->temp > 40) biwingData->temp = 40;
  90.     if (biwingData->temp == 40)
  91.         biwingData->speed -= 2;
  92.     else if (biwingData->temp > 35)
  93.         biwingData->speed -= 1;
  94.     if (speed != biwingData->speed) {
  95.         strcpy(part.name, "biwing:biwing_leftwing");
  96.         part.attrib = PART_ROT;
  97.         part.coords.rot.z = (3 * biwingData->speed);
  98.         composeServerEventMessage(&ev, CHANGE, CHG_PART, theBiwing->name, &part);
  99.         broadcast(SERVEREVENT, sizeof(serverEventMsg_t), &ev);
  100.         strcpy(part.name, "biwing:biwing_rightwing");
  101.         part.coords.rot.z = -(3 * biwingData->speed);
  102.         composeServerEventMessage(&ev, CHANGE, CHG_PART, theBiwing->name, &part);
  103.         broadcast(SERVEREVENT, sizeof(serverEventMsg_t), &ev);
  104.         strcpy(panel.name, "biwing:speed");
  105.         panel.type = PANEL_VALUE;
  106.         panel.value = biwingData->speed;
  107.         composeServerEventMessage(&ev, CHANGE, CHG_PANEL, theBiwing->type, &panel);
  108.         sendMessage(theBiwing->socket, SERVEREVENT, sizeof(serverEventMsg_t), &ev);
  109.         }
  110.     if (temp != biwingData->temp) {
  111.         panel.type = PANEL_VALUE;
  112.         strcpy(panel.name, "biwing:temp");
  113.         panel.value = biwingData->temp;
  114.         composeServerEventMessage(&ev, CHANGE, CHG_PANEL, theBiwing->type, &panel);
  115.         sendMessage(theBiwing->socket, SERVEREVENT, sizeof(serverEventMsg_t), &ev);
  116.         }
  117.     if (shields != biwingData->shields) {
  118.         panel.type = PANEL_VALUE;
  119.         strcpy(panel.name, "biwing:shield");
  120.         panel.value = biwingData->shields;
  121.         composeServerEventMessage(&ev, CHANGE, CHG_PANEL, theBiwing->type, &panel);
  122.         sendMessage(theBiwing->socket, SERVEREVENT, sizeof(serverEventMsg_t), &ev);
  123.         }
  124.         
  125.     strcpy(move.name, theBiwing->name);
  126.     memcpy(&move.coords, &theBiwing->coords, sizeof(viewpoint));
  127.     composeServerEventMessage(&ev, MOVE, 0, NULL, &move);
  128.     broadcast(SERVEREVENT, sizeof(serverEventMsg_t), &ev);
  129.     moveObjects(biwingData->missile);
  130.     moveObjects(biwingData->mine);
  131.     }
  132.  
  133. int calcVectors(viewpoint *the_ship, int x, int y,int speed) {
  134.  
  135.     double divisor = speed;
  136.     if (divisor == 0) divisor = 1;
  137.     the_ship->rot.y -= (x /100.0);
  138.     the_ship->rot.x = speed * y/170.0;
  139.     the_ship->rot.z = -x/20.0;
  140.     if (the_ship->rot.y > 359) the_ship->rot.y -= 360;
  141.     if (the_ship->rot.y < 0) the_ship->rot.y += 360;
  142.     }
  143.  
  144. int checkBiwingCollision(object_t *theBiwing, biwingExt_t *biwingData,
  145.                                                 collideEvent_t *event) {
  146.  
  147.     object_t *theCollider;
  148.     serverEventMsg_t ev;
  149.     changePanel_t panel;
  150.     int shields;
  151.     theCollider = event->obj;
  152.     if (theCollider->parent == theBiwing)
  153.         return 0;
  154.     if (!strcmp(theCollider->type, "missile") ||
  155.                 !strcmp(theCollider->type, "mine")) {
  156.         biwingData->counter = 0;
  157.         if (biwingData->shields > 0)
  158.             biwingData->shields -= 25;
  159.         if (biwingData->shields < 0) {
  160.             biwingData->state = HIT;
  161.             biwingData->shields = 0;
  162.             }
  163.         panel.type = PANEL_VALUE;
  164.         strcpy(panel.name, "biwing:shield");
  165.         panel.value = biwingData->shields;
  166.         composeServerEventMessage(&ev, CHANGE, CHG_PANEL, theBiwing->type, &panel);
  167.         sendMessage(theBiwing->socket, SERVEREVENT, sizeof(serverEventMsg_t), &ev);
  168.         return 1;
  169.         }
  170.     return 0;
  171.     }
  172.  
  173. int updateBiwingScore(object_t *theBiwing, biwingExt_t *biwingData,
  174.                     termEvent_t *event) {
  175.     
  176.     serverEventMsg_t ev;
  177.     changePanel_t panel;
  178.  
  179.     biwingData->score += 100;
  180.     sprintf(panel.label, "%05d", biwingData->score);
  181.     strcpy(panel.name, "biwing:score");
  182.     panel.type = PANEL_LABEL;
  183.     composeServerEventMessage(&ev, CHANGE, CHG_PANEL, "biwing", &panel);
  184.     sendMessage(theBiwing->socket, SERVEREVENT, sizeof(ev), &ev);
  185.     }
  186.  
  187. int flyBiwing(object_t *theBiwing, biwingExt_t *biwingData,
  188.                                                                             clientEventMsg_t *event) {
  189.  
  190.     int FireButton;
  191.     serverEventMsg_t ev;
  192.     changePart_t part;
  193.     changePanel_t panel;
  194.     int speed = biwingData->speed;
  195.     memset(&ev, 0, sizeof(serverEventMsg_t));
  196.     memset(&part, 0, sizeof(changePart_t));
  197.     switch (biwingData->state) {
  198.         case FLYING:
  199.             FireButton = GetButtonPress(event->buttons, &biwingData->speed);
  200.             calcVectors(&(theBiwing->coords), event->x, event->y, biwingData->speed);
  201.             if (speed != biwingData->speed) {
  202.                 strcpy(part.name, "biwing:biwing_leftwing");
  203.                 part.attrib = PART_ROT;
  204.                 part.coords.rot.z = (3 * biwingData->speed);
  205.                 composeServerEventMessage(&ev, CHANGE, CHG_PART, theBiwing->name,
  206.                                                                                 &part);
  207.                 broadcast(SERVEREVENT, sizeof(serverEventMsg_t), &ev);
  208.                 strcpy(part.name, "biwing:biwing_rightwing");
  209.                 part.coords.rot.z = -(3 * biwingData->speed);
  210.                 composeServerEventMessage(&ev, CHANGE, CHG_PART, theBiwing->name,
  211.                                                                                 &part);
  212.                 broadcast(SERVEREVENT, sizeof(serverEventMsg_t), &ev);
  213.                 panel.type = PANEL_VALUE;
  214.                 strcpy(panel.name, "biwing:speed");
  215.                 panel.value = biwingData->speed;
  216.                 composeServerEventMessage(&ev, CHANGE, CHG_PANEL, theBiwing->type,
  217.                                                                                 &panel);
  218.                 sendMessage(theBiwing->socket, SERVEREVENT, sizeof(serverEventMsg_t),
  219.                                                     &ev);
  220.                 }
  221.             if (FireButton)
  222.                 initSubObject(theBiwing, biwingData->missile);
  223.             if (event->keys == XK_m)
  224.                 initSubObject(theBiwing, biwingData->mine);
  225.             break;
  226.         case HIT:
  227.             if (biwingData->counter < 100) {
  228.                 biwingData->counter++;
  229.                 memset(&ev, 0, sizeof(serverEventMsg_t));
  230.                 strcpy(part.name, "biwing:biwing_leftwing");
  231.                 part.attrib = PART_ROT | PART_LOC;
  232.                 part.coords.rot.z = (5 * biwingData->counter);
  233.                 part.coords.loc.x = -10 + -(5 * biwingData->counter);
  234.                 part.coords.loc.y = -10;
  235.                 composeServerEventMessage(&ev, CHANGE, CHG_PART, theBiwing->name,
  236.                                                                                 &part);
  237.                 broadcast(SERVEREVENT, sizeof(serverEventMsg_t), &ev);
  238.                 strcpy(part.name, "biwing:biwing_rightwing");
  239.                 part.coords.rot.z = -(5 * biwingData->counter);
  240.                 part.coords.loc.x = 10 + (5 * biwingData->counter);
  241.                 composeServerEventMessage(&ev, CHANGE, CHG_PART, theBiwing->name,
  242.                                                                                 &part);
  243.                 broadcast(SERVEREVENT, sizeof(serverEventMsg_t), &ev);
  244.                 }
  245.             else {
  246.                 biwingData->state = FLYING;
  247.                 memset(&part, 0, sizeof(changePart_t));
  248.                 strcpy(part.name, "biwing:biwing_leftwing");
  249.                 part.attrib = PART_ROT | PART_LOC;
  250.                 part.coords.loc.x = -10;
  251.                 part.coords.loc.y = -10;
  252.                 composeServerEventMessage(&ev, CHANGE, CHG_PART, theBiwing->name,
  253.                                                                                 &part);
  254.                 broadcast(SERVEREVENT, sizeof(serverEventMsg_t), &ev);
  255.                 strcpy(part.name, "biwing:biwing_rightwing");
  256.                 part.coords.loc.x = 10;
  257.                 composeServerEventMessage(&ev, CHANGE, CHG_PART, theBiwing->name,
  258.                                                                                 &part);
  259.                 broadcast(SERVEREVENT, sizeof(serverEventMsg_t), &ev);
  260.                 }
  261.             break;
  262.         default:
  263.             break;
  264.         }
  265.     }
  266.  
  267. int addBiwingExtensions(object_t *theObject) {
  268.  
  269.     biwingExt_t *temp = NULL;
  270.     int i;
  271.     char name[50];
  272.     if ((temp = (biwingExt_t *)malloc(sizeof(biwingExt_t))) == NULL) {
  273.         fprintf(stderr, "Can't allocate space for biwing extension!\n");
  274.         return 0;
  275.         }
  276.     temp->shields = 40;
  277.     temp->speed = 0;
  278.     temp->temp = 0;
  279.     temp->state = FLYING;
  280.     temp->counter = 100;
  281.     temp->missile = NULL;
  282.     temp->mine = NULL;
  283.     temp->score = 0;
  284.     addEventHandler(theObject, CLIENT_NOTIFY_EVENT, temp, flyBiwing);
  285.     addEventHandler(theObject, MOVE_EVENT, temp, moveBiwing);
  286.     addEventHandler(theObject, COLLISION_NOTIFY_EVENT, temp, checkBiwingCollision);
  287.     addEventHandler(theObject, TERM_NOTIFY_EVENT, temp, updateBiwingScore);
  288.     theObject->collider = createCollider();
  289.     theObject->collider->obj = theObject;
  290.     theObject->collider->type = OBJ_DYNAMIC;
  291.     theObject->collider->boundType = BOX;
  292.     theObject->collider->box[0].x = -10;
  293.     theObject->collider->box[0].y = -10;
  294.     theObject->collider->box[0].z = -20;
  295.     theObject->collider->box[1].x = 10;
  296.     theObject->collider->box[1].y = 10;
  297.     theObject->collider->box[1].z = 20;
  298.     addCollider(theObject->collider);
  299.     for (i=0; i< MAX_MISSILES; i++) {
  300.         sprintf(name, "%s-missile%1d\0", theObject->name, i);
  301.         addObject(&temp->missile, createLocalObject(theObject, name, "missile", theObject->socket));
  302.         }
  303.     for (i=0; i< MAX_MINES; i++) {
  304.         sprintf(name, "%s-mine%1d\0", theObject->name, i);
  305.         addObject(&temp->mine, createLocalObject(theObject, name, "mine", theObject->socket));
  306.         }
  307.     }
  308.  
  309. int installBiwingExtensions() {
  310.  
  311.     installEventHandlerCreator("biwing", addBiwingExtensions);
  312.     installMissileExtensions();
  313.     installMineExtensions();
  314.     }
  315.  
  316.